package com.itcast.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcast.reggie.common.R;
import com.itcast.reggie.dto.OrdersDto;
import com.itcast.reggie.entity.AddressBook;
import com.itcast.reggie.entity.OrderDetail;
import com.itcast.reggie.entity.Orders;
import com.itcast.reggie.service.AddressBookService;
import com.itcast.reggie.service.OrderDetailService;
import com.itcast.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hehehe
 * @ClassName OrderController
 * @Description TODO
 * @date 2022/12/5/005 10:01
 * @Version 1.0
 */

@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping("/submit")
    private R<String> submit(@RequestBody Orders order) {
        log.info(order.toString());
        orderService.submit(order);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    private R<Page<OrdersDto>> page(int page, int pageSize) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        orderService.page(pageInfo, queryWrapper);
        List<Orders> orders = pageInfo.getRecords();
        List<OrdersDto> ordersDtos = orders.stream().map((order) -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order, ordersDto);
            LambdaQueryWrapper<OrderDetail> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(OrderDetail::getOrderId, order.getId());
            List<OrderDetail> list = orderDetailService.list(queryWrapper1);
            ordersDto.setOrderDetails(list);
            AddressBook addressBook = addressBookService.getById(order.getAddressBookId());
            order.setAddress((addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
                    + (addressBook.getCityName() == null ? "" : addressBook.getCityName())
                    + (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
                    + (addressBook.getDetail() == null ? "" : addressBook.getDetail()));
            ordersDto.setPhone(addressBook.getPhone());
            ordersDto.setConsignee(addressBook.getConsignee());
            return ordersDto;
        }).collect(Collectors.toList());
        Page<OrdersDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        dtoPage.setRecords(ordersDtos);
        return R.success(dtoPage);
    }
}
